//
//  BorrowRecodeController.m
//  51易借
//
//  Created by 张睿 on 2017/8/28.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "BorrowRecodeController.h"
#import "ReadyFinishViewController.h"
#import "LoanModel.h"
#import "ImmediatelyRepayController.h"
#import "LoanRecordtypeOneTableViewCell.h"
#import "LoanRecordtypeTwoTableViewCell.h"
@interface BorrowRecodeController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
@property (nonatomic,strong)LoanModel * model;
@property (nonatomic, weak)UIView * topView;
@property (nonatomic, weak)UIView * inputView;
@property (nonatomic, strong)UILabel * borrowSumLabel;
@property (nonatomic, strong)UILabel * borrowSumValueLabel;
@property (nonatomic, weak)UIButton * stateImageButton;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UILabel * borrowDateLabel;
@property (nonatomic, strong)UILabel * borrowDateValueLabel;
@property (nonatomic, strong)UILabel * repaymentDateLabel;
@property (nonatomic, strong)UILabel * repaymentDateValueLabel;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UILabel * repaymentLabel;
@property (nonatomic, strong)UILabel * repaymentValueLabel;
@property (nonatomic, strong)UIView * verticalLineView;
@property (nonatomic, weak)UIButton * repayButton;
@property (nonatomic,strong)UITableView * tableView;
@property (nonatomic,strong)NSArray * dataArr;

@property (nonatomic,strong)UIImageView * faceView;
@property (nonatomic,strong)UILabel * labelView;

@end

@implementation BorrowRecodeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BACK_COLOR;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToRepay) name:@"pushToRepay" object:nil];
    
    [self setupNavBar];
    [self setupUI];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getRecords];
}




- (void)setupNavBar{
    
    self.title = @"借款记录";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLastView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
    UIButton * rightButton = [[UIButton alloc] init];
    
    rightButton.frame = CGRectMake(0 * SCREENH6, 0 * SCREENH6, 52 * SCREENH6, 20 * SCREENH6);
//    [rightButton setImage:[UIImage imageNamed:@"客服"] forState:UIControlStateNormal];
    [rightButton setTitle:@"已结清" forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"已结清框"] forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:11 * SCREENH6];
    
    [rightButton addTarget:self action:@selector(pushToReadyVC) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}


- (void)setupUI{
    
    UIView * topView = [UIView new];
    topView.frame = CGRectMake(0, 0, ScreenWidth, 64);
    topView.backgroundColor = TOTAL_COLOR;
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREENH_HEIGHT-64)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.separatorStyle = NO;
    
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LoanRecordtypeOneTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"one"];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LoanRecordtypeTwoTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"two"];
    
    
    self.faceView = [UIImageView new];
    self.faceView.image = [UIImage imageNamed:@"无"];
//    self.faceView.hidden = YES;
    
    self.labelView = [UILabel new];
    self.labelView.font = [UIFont systemFontOfSize:15*SCREENH6];
    self.labelView.textColor = [UIColor grayColor];
    self.labelView.text = @"目前无待还款账单";
//    self.labelView.hidden = YES;
    
    [self.view addSubview:self.faceView];
    [self.view addSubview:self.labelView];
    
    [self.faceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view).offset(-10 * SCREENH6);
        make.height.mas_equalTo(82 * SCREENH6);
        make.width.mas_equalTo(94 * SCREENH6);
        
    }];
    [self.labelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.faceView.mas_bottom).offset(20 * SCREENH6);
        make.height.mas_equalTo(15 * SCREENH6);
        
    }];
}



- (void)pushToReadyVC{
    
    HIDDEN_TAB;
    
    ReadyFinishViewController * readyVC = [ReadyFinishViewController new];
    
    [self.navigationController pushViewController:readyVC animated:YES];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LoanModel * model = self.dataArr[indexPath.row];
    if([model.unionStatus intValue]==4||[model.unionStatus intValue]==5||[model.unionStatus intValue]==6)
    {
        return 55*3*SCREENH6+8;
    }else
    {
        return 55*2*SCREENH6+8;
    }
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LoanModel * model = self.dataArr[indexPath.row];
    
//    NSLog(@"====%d",[model.unionStatus intValue]);
    
    if([model.unionStatus intValue]==4||[model.unionStatus intValue]==5||[model.unionStatus intValue]==6)
    {
        LoanRecordtypeTwoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"two" forIndexPath:indexPath];

        cell.model = model;
        return cell;
    }else
    {
        LoanRecordtypeOneTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"one" forIndexPath:indexPath];
        cell.model = model;

        return cell;
    }
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HIDDEN_TAB;
    LoanModel * model = self.dataArr[indexPath.row];
    ImmediatelyRepayController * vc = [ImmediatelyRepayController new];
//    vc.hidesBottomBarWhenPushed = YES;
    vc.loanID = model.applyNo;
    vc.borrow = @"1";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)pushToRepay{
    
    HIDDEN_TAB;
    LoanModel * model = self.dataArr[0];
    ImmediatelyRepayController * vc = [ImmediatelyRepayController new];
    //    vc.hidesBottomBarWhenPushed = YES;
    vc.loanID = model.applyNo;
    vc.borrow = @"1";
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)backToLastView{
    
    if ([self.frome isEqualToString:@"1"]) {
        
        self.tabBarController.selectedIndex = 2;
    }else{
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)getRecords
{
    ParaModel*para = [ParaModel new];
    para.limit = @(10);
    para.page = @(1);
    
    [[NetworkTool shareManager] POST:[Interfacefile loadBorrowingRecordList] Param:[para toDictionary] andWaitStr:@"" waitView:self.view success:^(id  _Nullable responseObject) {
        
//        NSLog(@"==%@",responseObject);
        
        if([[responseObject objectForKey:@"code"]intValue]==0)
        {
            self.dataArr = [LoanModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"data"] error:nil];

            if (self.dataArr.count > 0) {
               
                self.faceView.hidden = YES;
                self.labelView.hidden = YES;
                self.tableView.hidden = NO;
                [self.tableView reloadData];
            }else{
                
                self.faceView.hidden = NO;
                self.labelView.hidden = NO;
                self.tableView.hidden = YES;
            }
            
            
        }else{
            self.dataArr = nil;
            [self.tableView reloadData];

            [self showHint:[responseObject objectForKey:@"message"]];
            
            
        }
        
    } andFailure:^(NSError * _Nonnull error) {
        self.dataArr = nil;
        [self.tableView reloadData];
        [self showHint:@"网络错误"];
    }];
    
}

@end
